var ns, app;
app = new Vue({
    el: '#app',
    data: {
        ismoreshow: false,
        form: {
            ID: 0,
            imagelist: [],
            processlist: [],
        },
        timeOutList:[],
        task_imgList: [],
        total: false,
        isviewdetail: true,
        num: 6,
        rateValueist: ['极差', '差评', '一般 ', '优秀', '好评'],
        showorder: false,
        showproess:false,
        //收费记录
        moneylist:[],
        //领料记录 未关联生成出库单。则显示为：x 申请中   如果用户存在多次申请，则显示当前的领用数量。不显示第二次申请的状态
        materiallist: [],
        productlist:[],
        rateList: [],
        callBackData:[],
        typetj:0,//1-工单统计
        fhlist:[],//物料返还数据
        operation: {
            canVisit: false,
            canClose: false,
            canSend: false,
            canAccept: false,
            canAfreshSend: false,
            canChuidan: false,
            canRefuse: false,
            canAcceptOrder: false,
            canComplete: false,
            canHandle: false
        },
        servicetypename:'',//工单类型
        servicetypeid:'',//工单类型id
        showFWPopover:false,//控制服务卡列表弹框显示
    },
    methods: {
        showOrderAll: function () {
            var that = this;
            that.showorder = !that.showorder;
        },
        showProessAll: function () {
            var that = this;
            that.showproess = !that.showproess;
        },
        showOrderFw: function () {
            var that = this;
            that.showFWPopover = !that.showFWPopover;
        },
        get_data: function() {
            var that = this;
            var options = {};
            options.ID = that.form.ID;    
            options.action = "getnewservicedetail";
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (succeed) {
                    that.form = data.form;
                    that.timeOutList=data.timeOutList;
                    that.task_imgList = data.form.imagelist;
                    that.moneylist = data.form.feeLists;
                    that.materiallist = data.form.productList;
                    that.rateList = data.form.rateList;
                    that.callBackData = data.form.callBackData;
                    if (that.form.ServiceAppointTime && that.form.ServiceAppointEndTime) {
                        var moment1 = moment(new Date(that.form.ServiceAppointTime), 'MM/D/YYYY');
                        var moment2 = moment(new Date(that.form.ServiceAppointEndTime), 'MM/D/YYYY');
                        var IsSameDay = moment1.isSame(moment2, 'day');
                        if (IsSameDay) {
                            that.form.ServiceAppointEndTime = ns.DateFormatYY(that.form.ServiceAppointEndTime);
                        }
                    }
                    if (that.form.canHandle) {
                        that.get_fhproject();//查询是否可以物料返还
                    }
                    api.sendEvent({
                        name: 'is_hidden_right_gongdan',
                        extra: {
                            keyId: 'right_gongdan',
                            IsHidden: data.form.canTeamwork?data.form.canTeamwork:false
                        }
                    });
                    setTimeout(function () {
                        app.get_operation();
                    }, 10)
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        get_operation: function () {
            var that = this;
            var options = {};
            options.action = "getserviceoperation";
            options.servicetype = that.form.FinalServiceTypeID;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.operation = data.operation;
                }else{
                }
            });
        },
        img_browser: function(url) {
            var that = this;
            var imgurls = [];
            imgurls.push(url);
            imageBrowser.openImages({
                imageUrls: imgurls,
                showList: false,
                activeIndex: 0
            });
        },
        tel_phone: function(item) {
            api.call({
                type: 'tel_prompt',
                number: item.phonenumber
            });
        },
        tel_phone2: function(phonenumber) {
            api.call({
                type: 'tel_prompt',
                number: phonenumber
            });
        },
        open_mask_more: function() {
            var that = this;
            that.ismoreshow = true;
        },
        hide_mask_more: function() {
            var that = this;
            that.ismoreshow = false;
        },
        no_open: function() {
            api.alert({
                title: '提示',
                msg: '功能暂时未开放,敬请期待',
            }, function(ret, err) {});
        },
        openSchedule: function() {
            ns.openWin('home_schedule_frm', '日程');
        },
        openGongdan: function() {
            ns.openWin('home_task_con_frm', '任务');
        },
        openWin: function(item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
        do_grab: function() {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "grabservice";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },

        //判断是否受理
        do_isShouli:function(){
            var that=this;
            if(!that.form.ServiceTypeIsDeep){
                ns.openWin('neibubaoshi_typelist', '工单类型选择',{
                    placetype:1,//1-微信公众号改工单类型
                    gongdanType:that.form.FinalServiceTypeID
                });
            }else{
                that.doAcceptance();
            }
        },
        //修改工单类型
        do_editType(){
            var that = this;
            var options={};
            options.ID=that.form.ID;
            options.TypeID=that.servicetypeid;
            options.TypeName=that.servicetypename;
            options.action="editcustomerservice";
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.doAcceptance();
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })

        },

        //受理
        doAcceptance: function () {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "customerserviceshouli";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: "操作成功",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    if (!data.status) {
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //派单
        do_canpublish: function(type) {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function (succeed, data, err) {
                if (data.status) {
                    if (data.ServiceStatus == that.form.status) {
                        ns.openWin('chooseserviceuser_frm', '接单人选择', {
                            source: 'transfer',
                            id: that.form.ID,
                            type: type,
                            ServiceType: that.form.FinalServiceTypeID
                        });
                    }
                    else {
                        api.toast({
                            msg: "该工单状态已改变",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });     
        },
        //接单
        doAccpetOrder: function () {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "acceptservice";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: "操作成功",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    if (!data.status) {
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });

        },
        do_accpet: function (isProcess, type) {
            var that = this;
            var title = '录入处理记录';
            if (type == 2) {
                title = '录入完工记录';
            }
            if (type == 3) {
                title = '录入回访记录';
            }
            var options = {};
            options.ID = that.form.ID;
            options.action = "acceptservice";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    if (isProcess) {
                        
                        ns.openWin('renwugongdancomplete_frm', title, {
                            id: that.form.ID,
                            issuggestion: that.form.issuggestion,
                            type: type,//1 处理  2 办结 3 回访
                        });
                    } else {
                        that.get_data();
                    }
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        do_complete: function(type) {
            var that = this;
            var title = '录入处理记录';
            if (type == 2) {
                title = '录入完工记录';
            }
            if (type == 3) {
                title = '录入回访记录';
            }
            var options = {};
            options.ID = that.form.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function (succeed, data, err) {
                if (data.status) {
                    if (data.ServiceStatus == that.form.status) {
                        ns.openWin('renwugongdancomplete_frm', title, {
                            id: that.form.ID,
                            issuggestion: that.form.issuggestion,
                            type: type,//1 处理  2 完工  3 回访
                            ServiceType:  that.form.FinalServiceTypeID
                        });
                    }
                    else {
                        api.toast({
                            msg: "该工单状态已改变",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
       
        },
        //工单回访
        do_returnvisit: function() {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function (succeed, data, err) {
                if (data.status) {
                    if (data.ServiceStatus == that.form.status) {
                        ns.openWin('renwugongdanreturnvisit', '录入回访记录', {
                            id: that.form.ID,
                            issuggestion: that.form.issuggestion,
                            type: 3,//1 处理  2 完工  3 回访
                            ServiceType:  that.form.FinalServiceTypeID
                        });
                    }
                    else {
                        api.toast({
                            msg: "该工单状态已改变",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        //关单
        doCloseOrder: function () {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "serviceclose";
            api.confirm({
                title: '提示',
                msg: '工单关闭后，不能再进行【回访】以外的其他操作，请确认工单是否已经处理完成？',
                buttons: ['确认关闭', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        ns.post(options, function (succeed, data, err) {
                            if (succeed) {
                                if (!data.status) {
                                    api.toast({
                                        msg: data.Msg,
                                        duration: 2000,
                                        location: 'bottom'
                                    });
                                }
                                that.get_data();
                                api.sendEvent({
                                    name: 'reloadservicelist',
                                });
                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }, {
                            toast: true,
                            toastmsg: '提交中'
                        });
                    }
                }
            });

           
        },
        //拒绝
        doRefuseOrder: function () {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "refuseservice";
            api.confirm({
                title: '提示',
                msg: '拒绝接受该工单，工单将流转至派单人。是否确认拒绝？',
                buttons: ['确认拒绝', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        ns.post(options, function (succeed, data, err) {
                            if (succeed) {
                                if (data.status) {
                                    api.toast({
                                        msg: "操作成功",
                                        duration: 2000,
                                        location: 'bottom'
                                    });
                                }
                                if (!data.status) {
                                    api.toast({
                                        msg: data.Msg,
                                        duration: 2000,
                                        location: 'bottom'
                                    });
                                }
                                that.get_data();
                                api.sendEvent({
                                    name: 'reloadservicelist',
                                });
                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }, {
                            toast: true,
                            toastmsg: '提交中'
                        });
                    }
                }
            });
           
        },
        //催单
        doUrgeOrder: function () {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "jpushuser";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: "操作成功",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    if (!data.status) {
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //协同
        open_xietong: function() {
            var that = this;
            ns.openWin('chooseserviceuser_frm', '协同处理', {
                source: 'transfer',
                id: that.form.ID,
                type: 2,
                is_xietong: true,
                ServiceType: that.form.FinalServiceTypeID
            });
        },
        //转交
        open_transfer: function() {
            var that = this;
            ns.openWin('chooseserviceuser_frm', '工单转交', {
                source: 'transfer',
                id: that.form.ID,
                type: 3,
                ServiceType: that.form.FinalServiceTypeID
            });
        },
        do_returnback: function() {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "doreturnbackservice";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        do_open_fee: function() {
            var that = this;

            //查询当前工单状态
            var options = {};
            options.ID = that.form.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function (succeed, data, err) {
                if (data.status) {
                    if (data.ServiceStatus == that.form.status) {
                        ns.openWin('customerservice_feelist_frm', '有偿收费', {
                            id: that.form.ID
                        });
                    }
                    else {
                        api.toast({
                            msg: "该工单状态已改变",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
          
        },
        do_open_product: function() {
            var that = this;
            ns.openWin('customerservice_productlist_frm', '领料详情', {
                id: that.form.ID
            });
        },
        do_add_fee: function () {
            var that = this;
            ns.openWin('renwugongdan_addfee_frm', '收费详情', {
                id: app.form.ID
            });
        },
        do_add_product: function () {
            var that = this;
            //查询当前工单状态
            var options = {};
            options.ID = that.form.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function (succeed, data, err) {
                if (data.status) {
                    if (data.ServiceStatus == that.form.status) {
                        ns.openWin('chooseckproduct_frm', '物料领用申请', {
                            ID: that.form.ID,
                        });
                    }
                    else {
                        api.toast({
                            msg: "该工单状态已改变",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
          
        },
        do_return_product: function () {
            var that = this;

            //查询当前工单状态
            var options = {};
            options.ID = that.form.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function (succeed, data, err) {
                if (data.status) {
                    if (data.ServiceStatus == that.form.status) {
                        ns.openWin('choosefhproduct_frm', '物料返还申请', {
                            ID: that.form.ID,
                        });
                    }
                    else {
                        api.toast({
                            msg: "该工单状态已改变",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        // 查询是否有物料返还数据，无则不显示返还按钮
        get_fhproject: function () {
            var that = this;
            that.fhlist = [];
            var options = {
                action: 'getserviceproductlist',
                ServiceID: that.form.ID,
                KeyWords: '',
            };
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.fhlist = data.list;
                } 
            });
        },

    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    imageBrowser = api.require('imageBrowser');
    app.form.ID = api.pageParam.id || 0;
    app.isviewdetail = api.pageParam.isviewdetail || false;
    app.typetj=api.pageParam.type;
    app.get_data();
    api.addEventListener({
        name: 'updata_task_con'
    }, function(ret, err) {
        if (ret) {
            app.get_data();
            app.ismoreshow = false;
        } else {
            api.toast({
                msg: err,
                duration: 2000,
                location: 'bottom'
            });

        }
    });

    //协同
    api.addEventListener({
        name: 'do_open_chooseserviceuser',
    }, function (ret, err) {
        var options = {};
        options.ID = app.form.ID;
        options.action = "getcustomerservicestatus";
        ns.post(options, function (succeed, data, err) {
            if (data.status) {
                if (data.ServiceStatus == app.form.status) {
                    ns.openWin('chooseserviceuser_frm', '协同人员选择', {
                        source: 'transfer',
                        id: app.form.ID,
                        type: 2,
                        is_xietong: true,
                        ServiceType: app.form.FinalServiceTypeID,
                    });
                }
                else {
                    api.toast({
                        msg: "该工单状态已改变",
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                app.get_data();
            } else {
                api.toast({
                    msg: data.Msg,
                    duration: 2000,
                    location: 'bottom'
                });
            }
        });    
    });
    api.addEventListener({
        name: 'dosaveservicetype'
    }, function (ret) {
        app.servicetypename = ret.value.item.text;
        app.servicetypeid = ret.value.item.id;
        app.do_editType();

    })

    api.sendEvent({
        name: 'is_task_read',
        extra: {
            key1: app.form.ID,
        }
    });

};
